<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Webpack基础配置(一) | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="初始化node项目执行命令：yarn init -y并生成package.json文件 下载webpack执行命令：yarn add webpack webpack-cli --dev 在项目根目录创建src文件夹目录结构如图： 文件内容如图：  注：webpack默认支持AMD、CMD、commonJS、ES6方式模块的导入导出。  webpack命令直接在终端执行webpack命令，都是全局命">
<meta property="og:type" content="article">
<meta property="og:title" content="Webpack基础配置(一)">
<meta property="og:url" content="http://example.com/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="初始化node项目执行命令：yarn init -y并生成package.json文件 下载webpack执行命令：yarn add webpack webpack-cli --dev 在项目根目录创建src文件夹目录结构如图： 文件内容如图：  注：webpack默认支持AMD、CMD、commonJS、ES6方式模块的导入导出。  webpack命令直接在终端执行webpack命令，都是全局命">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ee13d6e638df40819fc87eb4bfa7d70f~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2117544a9a3440f491bb1e41bdd5e64c~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4f43a5ff9494404fac63d2ae699249d6~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0f17ab599d07409dbb534ffc90a30b35~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4744ca9adddf4b01a59d8cad482bed90~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/afa4d06314c845c09bff448cb3a22293~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ab8ea0a604284528801b130bca414c29~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/028eb19aea04492790ae90a185a12111~tplv-k3u1fbpfcp-watermark.image">
<meta property="article:published_time" content="2020-12-20T11:23:44.000Z">
<meta property="article:modified_time" content="2020-12-20T11:26:49.664Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ee13d6e638df40819fc87eb4bfa7d70f~tplv-k3u1fbpfcp-watermark.image">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Webpack基础配置-一" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/" class="article-date">
  <time class="dt-published" datetime="2020-12-20T11:23:44.000Z" itemprop="datePublished">2020-12-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Webpack基础配置(一)
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="初始化node项目"><a href="#初始化node项目" class="headerlink" title="初始化node项目"></a>初始化node项目</h2><p>执行命令：<code>yarn init -y</code>并生成package.json文件</p>
<h2 id="下载webpack"><a href="#下载webpack" class="headerlink" title="下载webpack"></a>下载webpack</h2><p>执行命令：<code>yarn add webpack webpack-cli --dev</code></p>
<h2 id="在项目根目录创建src文件夹"><a href="#在项目根目录创建src文件夹" class="headerlink" title="在项目根目录创建src文件夹"></a>在项目根目录创建src文件夹</h2><p>目录结构如图：<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ee13d6e638df40819fc87eb4bfa7d70f~tplv-k3u1fbpfcp-watermark.image"></p>
<p>文件内容如图：<br><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2117544a9a3440f491bb1e41bdd5e64c~tplv-k3u1fbpfcp-watermark.image"><br><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4f43a5ff9494404fac63d2ae699249d6~tplv-k3u1fbpfcp-watermark.image"><br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0f17ab599d07409dbb534ffc90a30b35~tplv-k3u1fbpfcp-watermark.image"></p>
<blockquote>
<p><code>注：webpack默认支持AMD、CMD、commonJS、ES6方式模块的导入导出。</code></p>
</blockquote>
<h2 id="webpack命令"><a href="#webpack命令" class="headerlink" title="webpack命令"></a>webpack命令</h2><p>直接在终端执行webpack命令，都是<code>全局命令</code>。</p>
<p>在项目根目录下，执行<code>.\node_modules\.bin\webpack</code>命令才是<code>局部命令</code>;<br>或者在<code>package.json的scripts属性</code>中配置的webpack命令也是<code>局部命令</code>。</p>
<p>如图：<br><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4744ca9adddf4b01a59d8cad482bed90~tplv-k3u1fbpfcp-watermark.image"></p>
<h2 id="使用webpack命令"><a href="#使用webpack命令" class="headerlink" title="使用webpack命令"></a>使用webpack命令</h2><p>在终端执行  .\node_modules.bin\webpack 或者 yarn build命令</p>
<p>webpack默认会在项目根目录下查找<code>weboack.config.js</code>文件，<br>没有weboack.config.js文件则直接将src/index.js当作<code>入口文件</code>进行打包。</p>
<h2 id="输出文件"><a href="#输出文件" class="headerlink" title="输出文件"></a>输出文件</h2><p>执行命令后，会在项目根目录下生成dist目录<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/afa4d06314c845c09bff448cb3a22293~tplv-k3u1fbpfcp-watermark.image"></p>
<h2 id="使用生成文件"><a href="#使用生成文件" class="headerlink" title="使用生成文件"></a>使用生成文件</h2><p>在src目录下创建idnex.html文件，并且引用生成的文件<br><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ab8ea0a604284528801b130bca414c29~tplv-k3u1fbpfcp-watermark.image"></p>
<p>发布页面后，查看运行结果<br><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/028eb19aea04492790ae90a185a12111~tplv-k3u1fbpfcp-watermark.image"></p>
<h2 id="链接"><a href="#链接" class="headerlink" title="链接"></a>链接</h2><p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6906486131920125959">下一篇 Webpack基础配置(二)</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/" data-id="ckjsoehcb0000fsr0bpu59dhq" data-title="Webpack基础配置(一)" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Webpack基础配置(二)
        
      </div>
    </a>
  
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/">Webpack基础配置(四)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/">Webpack基础配置(三)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/">Webpack基础配置(二)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/">Webpack基础配置(一)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>